public class LinkList implements IList{

    static class Linknode {
        public int val;
        public Linknode next;
        public Linknode prev;

        public Linknode(int val) {
            this.val = val;
        }


    }

    public Linknode head;
    public Linknode last;

    @Override
    public void addFirst(int data) {
        Linknode p = new Linknode(data);
        if(head ==  null) {
            head = last = p;
            return;
        }
        p.next = head;
        head.prev = p;
        head = p;
    }

    @Override
    public void addLast(int data) {

        Linknode p = new Linknode(data);
        if(head == null) {
            head = last = p;
            return;
        }
        last.next = p;
        p.prev = last;
        last = p;
    }

    @Override
    public void addIndex(int index, int data) {

        Linknode p = new Linknode(data);
        int l = size();
        if(index < 0 || index > l) {
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == l) {
            addLast(data);
            return;
        }
        Linknode t = head;
        while (index != 0) {
            t = t.next;
            index--;
        }
        p.next = t;

        p.prev = t.prev;
        t.prev.next = p;
        t.prev = p;
    }

    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }
        Linknode p = head;
        while (p != null) {
            if(p.val == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        if(last.val == key) {
            last = last.prev;
            last.next = null;
            return;
        }
        Linknode p = head;
        while (p != null) {
            if(p.val == key) {
                break;
            }
            p = p.next;
        }
        if (p == null) {
            return;
        }
        p.prev.next = p.next;
        p.next.prev = p.prev;

    }

    @Override
    public void removeAllKey(int key) {

        Linknode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        last = last.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }

    }

    @Override
    public int size() {
        if(head == null) {
            return 0;

        }
        Linknode p = head;
        int c = 0;
        while (p != null) {
            c++;
            p = p.next;
        }
        return c;
    }

    @Override
    public void clear() {

        if (head == null) {
            return;
        }
        Linknode p = head;
        while (p != null) {
            Linknode t = p.next;
            p.next = null;
            p.prev = null;
            p = t;
        }
        head = last = null;
    }

    @Override
    public void display() {

        if(head == null) {
            return;
        }
        Linknode p = head;
        while (p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
        System.out.println();
    }
}
